{*
SPDX-FileCopyrightText: © 2023 Olivier Meunier <olivier@neokraft.net>

SPDX-License-Identifier: AGPL-3.0-only
*}
{{ extends "./base" }}
{{ import "/_libs/forms" }}

{{ block title() }}Application Password{{ end }}

{{ block mainContent() }}
<h1 class="title text-h2">{{ yield title() }}</h1>

{{ if .Credential.IsDeleted -}}
<form action="{{ urlFor(`.`, `delete`) }}" method="POST">
  {{ yield csrfField() }}
  <input type="hidden" name="_to" value="{{ currentPath }}" />
  {{- yield message() content -}}
      Password will be removed in a few seconds&nbsp;
      <button class="btn-primary" name="cancel" value="1">Cancel</button>
  {{- end -}}
</form>
{{- end }}

{{- if .Passphrase -}}
<div class="my-4 p-4 bg-green-50 border border-green-600 rounded">
  <p>Your application password was created. Please copy the password bellow and write it down
  or store it in a password manager.</p>
  <p class="mt-4" data-controller="clipboard">
    Your password:
    <span class="bg-white mx-4 px-4 py-3 border border-gray-300 rounded font-bold"
     contenteditable spellcheck="false">{{ .Passphrase }}</span>
    <input type="hidden" value="{{ .Passphrase }}" data-clipboard-target="content">
    <button class="btn btn-primary"
     data-action="clipboard#copy">
     {{ yield icon(name="o-copy") }}
     Copy to clipboard</button>
  </p>
</div>
{{- end -}}

<h2 class="title text-h3">Properties</h2>


<form class="mb-4" action="{{ urlFor() }}" method="post">
  {{ yield formErrors(form=.Form) }}
  {{ yield csrfField() }}

  <div class="field field-h">
    <label>Password ID:</label>
    <div class="control">{{ .Credential.UID }}</div>
  </div>

  {{ yield textField(
    field=.Form.Get("name"),
    required=true,
    label="Name",
    class="field-h"
  ) }}

  {{ yield checkboxField(
    field=.Form.Get("is_enabled"),
    label="Enabled",
    class="field-h",
  ) }}

  <h2 class="title text-h3">Roles</h2>

  <p class="my-4 max-w-xl">
    You can limit the permissions granted to this password by choosing one or more
    group of permissions bellow.<br>
    Leave all the choices blank to grant all your permissions to this password.
  </p>

  {{ yield multiSelectField(
    field=.Form.Get("roles"),
    label="Roles",
    class="field-h",
  ) }}

  <p><button class="btn-primary" type="submit">Save</button></p>
</form>

{{ if !.Credential.IsDeleted -}}
<h2 class="title text-h3">Delete password</h2>
<form action="{{ urlFor(`.`, `delete`) }}" method="POST">
  {{ yield csrfField() }}
  <p><button class="btn-danger">Delete password</button></p>
</form>
{{- end }}

{{ end }}
